<template>
  <el-card class="box-card">
    <el-table :data="heroes">
      <el-table-column align="center" prop="_id" label="id" width="300" />
      <el-table-column align="center" prop="name" label="分类名" width="150" />
      <el-table-column align="center" label="图标" width="150">
        <template slot-scope="scope">
          <img :src="scope.row.avator" alt="" style="height: 3rem" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="$router.push(`/heroes/create/${scope.row._id}`)"
            type="info"
            size="medium"
          >
            编辑
          </el-button>
          <el-button @click="remove(scope.row)" type="danger" size="medium">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      heroes: []
    };
  },
  created() {
    this.fetch();
  },
  methods: {
    remove(row) {
      this.$confirm(`是否删除分类 "${row.name}"`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        const rs = await this.$http.delete(`rest/heroes/${row._id}`);
        rs;
        this.$message({
          type: "success",
          message: "删除成功!"
        });
        this.fetch();
      });
    },
    async fetch() {
      const rs = await this.$http.get("rest/heroes");
      this.heroes = rs.data;
      console.log(rs);
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
